import request from "superagent-bluebird-promise";

export class Builder {
    constructor(method, url) {
        this.request = request(method, window.appConfig.serverUrl + url.replace(/([^:])\/\//, '$1/')).set('X-Requested-With', 'XMLHttpRequest');
    }

    build() {
        return this.request;
    }
}

export default function fetch(method, url, body) {
    let req = new Builder(method, url).build();
    const promise = () => req
        .send(body)
        .type('json')
        .timeout(1000 * 60 * 10)
        .accept('json')
        .then((resp) => {
            debugger;
            if (resp.ok) {
                return JSON.parse(resp.text);
            }
        }, (error) => {

        });
    return promise()
}